/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.panels;

import java.util.List;
import java.util.Vector;

import nl.biz_e.gwt.baselib.ui.client.menu.IMenuItem;

import com.google.gwt.user.client.ui.SimplePanel;

/**
 * 
 * @author Joost A. Bloemsma
 */
public class BaseAppPanel extends SimplePanel implements IAppPanel {
    private PanelManager pnlMgr;
    private final Vector<IMenuItem> menuItems = new Vector<IMenuItem>();
    private boolean onBeforeGetMenuItemsFired = false;

    /**
     * Constructs a new BaseAppPanel instance
     */
    public BaseAppPanel() {
        super();
    }

    /**
     * {@inheritDoc}
     */
    public boolean prepareForClose() {
        return true;
    }

    /**
     * Adds an {@link IMenuItem} implementation to the internal list.
     * 
     * @param menuItem
     */
    protected void addMenuItem(IMenuItem menuItem) {
        menuItems.add(menuItem);
    }

    /**
     * Removes an {@link IMenuItem} implementation from the internal list.
     * 
     * @param menuItem
     */
    protected void removeMenuItem(IMenuItem menuItem) {
        menuItems.remove(menuItem);
    }

    /**
     * Clears the internal menuitem list
     */
    protected void removeAllMenuItems() {
        menuItems.clear();
    }

    /**
     * {@inheritDoc}
     */
    public List<IMenuItem> getMenuItems() {
        if (!onBeforeGetMenuItemsFired) {
            this.onBeforeGetMenuItems();
            onBeforeGetMenuItemsFired = true;
        }
        return menuItems;
    }

    /**
     * Called just before the first time {@link #getMenuItems()} is called.
     * Can be used to create the menuitems or something like that.
     */
    protected void onBeforeGetMenuItems() {
        // Nothing here, but can be overridden
        return;
    }

    /**
     * {@inheritDoc}
     */
    public void setPanelManager(PanelManager panelManager) {
        this.pnlMgr = panelManager;
    }

    /**
     * {@inheritDoc}
     */
    public PanelManager getPanelManager() {
        return this.pnlMgr;
    }
}
